package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.dao.impl.XingWenFengLeiDaoImpl;
import com.cyt.dao.impl.XingWengXiZhiTwoDaoImpl;
import com.cyt.entity.XingWenFengLei;
import com.cyt.entity.XingWengXiZhi;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;


public class AdminXingWenSuoYouServlet extends BaseServlet {
	/**
	 * 查询新闻所有
	 * @param request
	 * localhost:8080/shiping/adminXingWenSuoYou?reqName=getXingWenSY
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getXingWenSY(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int xingWengId=0;
		if(request.getParameter("xingWengId")!=null && !request.getParameter("xingWengId").equals("")) {
			xingWengId=Integer.parseInt(request.getParameter("xingWengId"));//要判断,不然会报数字转换异常
		}
		String xWBiaoTi=request.getParameter("xWBiaoTi");
		String xWengNeiRong=request.getParameter("xWengNeiRong");
		String xuHao=request.getParameter("xuHao");
		int xWLeiXingId=0;
		if(request.getParameter("xWLeiXingId")!=null && !request.getParameter("xWLeiXingId").equals("")) {
			xWLeiXingId=Integer.parseInt(request.getParameter("xWLeiXingId"));//要判断,不然会报数字转换异常
		}
		String xWXiangXi=request.getParameter("xWXiangXi");
		String renQi=request.getParameter("renQi");

		
		XingWengXiZhi xw = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi, "");
		XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();
		int page=Integer.parseInt(request.getParameter("page"));
		 int size=Integer.parseInt(request.getParameter("size"));//创建产品分类的实现类对象
	     List <XingWengXiZhi> xWSYLists= xinWenDaoi.getXingWenSuoYouPage(xw, page, size);//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(xWSYLists);//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
/**
 * localhost:8080/shiping/adminXingWenSuoYou?reqName=getXingWenSYTiao
 * 查表中数据条数
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void getXingWenSYTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int xingWengId=0;
		if(request.getParameter("xingWengId")!=null && !request.getParameter("xingWengId").equals("")) {
			xingWengId=Integer.parseInt(request.getParameter("xingWengId"));//要判断,不然会报数字转换异常
		}
		String xWBiaoTi=request.getParameter("xWBiaoTi");
		String xWengNeiRong=request.getParameter("xWengNeiRong");
		String xuHao=request.getParameter("xuHao");
		int xWLeiXingId=0;
		if(request.getParameter("xWLeiXingId")!=null && !request.getParameter("xWLeiXingId").equals("")) {
			xWLeiXingId=Integer.parseInt(request.getParameter("xWLeiXingId"));//要判断,不然会报数字转换异常
		}
		String xWXiangXi=request.getParameter("xWXiangXi");
		String renQi=request.getParameter("renQi");

		
		XingWengXiZhi xw = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi, "");
		XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();//创建产品分类的实现类对象
	     List <XingWengXiZhi> xWSYLists= xinWenDaoi.getXingWenSuoYou(xw);//查到的所有产品分类
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(xWSYLists.size());//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	/**
	 * 删除新闻
	 * localhost:8080/shiping/adminXingWenSuoYou?reqName=shanChuXinWen
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuXinWen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int xinWenId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
	    XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();//创建产品分类的实现类对象
	    int n =xinWenDaoi.shanChuXinWenZhu(xinWenId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * localhost:8080/shiping/adminXingWenSuoYou?reqName=shanChuXinWenS
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuXinWenS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();

		String ids=request.getParameter("ids");
	    int n =xinWenDaoi.shanXinWenS(ids);
	    
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 添加新闻
	 * localhost:8080/shiping/adminXingWenSuoYou?reqName=AddXinWen
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddXinWen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();
		String xWBiaoTi=request.getParameter("xWBiaoTi");
		String xWengNeiRong=request.getParameter("xWengNeiRong");
		String xuHao=request.getParameter("xuHao");
		String renQi=request.getParameter("renQi");
		int xWLeiXingId=0;
		if(request.getParameter("xWLeiXingId")!=null && !request.getParameter("xWLeiXingId").equals("")) {
			xWLeiXingId=Integer.parseInt(request.getParameter("xWLeiXingId"));//要判断,不然会报数字转换异常
		}
		String xWXiangXi=request.getParameter("xWXiangXi");
		XingWengXiZhi xw= new XingWengXiZhi(0, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi);
        System.out.println(xw);

		int n= xinWenDaoi.addXingWenXX(xw);
		
	    if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));

		}
		
		
	     
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 通过id查数据,前台用于数据回显
	 * localhost:8080/shiping/adminXingWenSuoYou?reqName=chaXingWenXiangQinId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaXingWenXiangQinId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();
		int xwxqId=Integer.parseInt(request.getParameter("xingWengId"));
		XingWengXiZhi xwxq= xinWenDaoi.chaXingWenXiangQinId(xwxqId);
        
		Gson gson=new Gson();
		String res =gson.toJson(xwxq);
	    out.print(res);
	    
	    
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * 修改新闻
	 *  localhost:8080/shiping/adminXingWenSuoYou?reqName=xiuGaiXingWenXiangQin 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiXingWenXiangQin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    XingWengXiZhiTwoDaoImpl xinWenDaoi=new XingWengXiZhiTwoDaoImpl();
		int xingWengId=0;
		if(request.getParameter("xingWengId")!=null && !request.getParameter("xingWengId").equals("")) {
			xingWengId=Integer.parseInt(request.getParameter("xingWengId"));
		}
		String xWBiaoTi=request.getParameter("xWBiaoTi");
		String xWengNeiRong=request.getParameter("xWengNeiRong");
		String xuHao=request.getParameter("xuHao");
		String renQi=request.getParameter("renQi");
		int xWLeiXingId=0;
		if(request.getParameter("xWLeiXingId")!=null && !request.getParameter("xWLeiXingId").equals("")) {
			xWLeiXingId=Integer.parseInt(request.getParameter("xWLeiXingId"));
		}
		String xWXiangXi=request.getParameter("xWXiangXi");
		XingWengXiZhi xwxz= new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi);
		int n =xinWenDaoi.xiuGaiXinWen(xwxz);
	    
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "修改成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "修改失败", ""));

		}
	    out.flush();
	    out.close();
	}
}
